package com.sharefang.aproject.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.sharefang.aproject.R;
import com.sharefang.aproject.adapter.XRecyclerViewAdapter;

/**
 * Created by Administrator on 2016/8/12.
 */

public abstract class RecyclerViewFragment extends Fragment {
    XRecyclerView xRecyclerView;

    RecyclerView.LayoutManager layoutManager;
    XRecyclerViewAdapter adapter;

    abstract void doRefresh();

    abstract void doLoadMore();

    abstract void setLayoutManager();

    abstract void setAdapter();

    abstract void onItemClick(View view, int position);

    abstract void onItemLongClick(View view, int position);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.xrecyclerview_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setLayoutManager();

        xRecyclerView = (XRecyclerView) view.findViewById(R.id.xrv_bf);
        xRecyclerView.setLayoutManager(layoutManager);
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
        xRecyclerView.setLoadingMoreEnabled(true);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }

            @Override
            public void onLoadMore() {
                doLoadMore();
            }
        });

        setAdapter();
        xRecyclerView.setAdapter(adapter);
        doRefresh();


        adapter.setOnItemClickListener(new XRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                RecyclerViewFragment.this.onItemClick(view,position);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                RecyclerViewFragment.this.onItemLongClick(view,position);
            }
        });
    }
}
